﻿@page "/Authentication"
@using System.IO;
@using Kite.Gateway.Application.Contracts
@using Kite.Gateway.Application.Contracts.Dtos.Authorization
@inject ToastService ToastService
@inject IAuthorizationAppService AuthorizationAppService
@inject IWebHostEnvironment WebHostEnvironment;
<div class="row">
    <ValidateForm Model="@Model" OnValidSubmit="@OnSaveAsync" OnInvalidSubmit="@OnInvalidSubmitAsync">
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.UseState" DisplayText="状态" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5">
                   <BootstrapInput @bind-Value="Model.Issuer" DisplayText="颁发者" IsTrim="true" />
             </div>
             <div class="col-5"  style="margin-top:5px;" >
                   <BootstrapInput @bind-Value="Model.Audience" DisplayText="观察者" IsTrim="true" />
             </div>
             <div class="col-5"  style="margin-top:5px;" >
                 
                 <BootstrapInputNumber @bind-Value="Model.ClockSkew" DisplayText="时间偏移(秒)" IsTrim="true" />
             </div>
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.ValidateIssuerSigningKey" DisplayText="是否验证签名" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.ValidateIssuer" DisplayText="是否验证颁发者" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.ValidateAudience" DisplayText="是否验证观察者" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.ValidateLifetime" DisplayText="是否验证失效时间" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.RequireExpirationTime" DisplayText="是否需要过期时间" OnText="开启" OffText="关闭"></Switch>
            </div>
            
             <div class="col-5"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.UseSSL" OnValueChanged="@OnUseSSLSwitch" DisplayText="是否启用SSL证书" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-5" style="margin-top:5px;">
                  <InputUpload Accept="" @bind-Value="@Model.CertificateFileName"  OnChange="@OnFileChange" DisplayText="证书文件(.pfx Or .cer)" />  
             </div>
             
            <div class="col-5" style="margin-top:5px;">
                 <BootstrapInput @bind-Value="Model.CertificatePassword" DisplayText="证书密码" IsDisabled="@(IsDisabledUseSSL)" IsTrim="true" />
             </div>
             <div class="col-5" style="margin-top:5px;">
                   <BootstrapInput @bind-Value="Model.SecurityKeyStr" DisplayText="自定义秘钥" IsDisabled="@IsDisabledSecurityKey" IsTrim="true" />
             </div>
            <div class="row" style="margin-top:10px;">
                <div class="col-5">
                    <Button ButtonType="ButtonType.Submit"  IsAsync="true" Color="Color.Primary">保 存</Button>
                </div>
            </div>
           </ValidateForm>
</div>


@code {

    [Parameter]
    public EventCallback OnClickCallback { get; set; }
    //表单数据
    private SaveAuthenticationDto Model { get; set; }

    private bool IsDisabledUseSSL { get; set; }
    private bool IsDisabledSecurityKey { get; set; }

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override async void OnInitialized()
    {
        base.OnInitialized();
        var result = await AuthorizationAppService.GetAuthenticationAsync();
        if (result.Code == 0)
        {
            Model = result.Data;
            IsDisabledUseSSL = Model.UseSSL ? false : true;
            IsDisabledSecurityKey = Model.UseSSL;
        }
    }
    private Task OnUseSSLSwitch(bool val)
    {
        IsDisabledSecurityKey = val;
        IsDisabledUseSSL = val ? false : true;
        return Task.FromResult(true);
    }
    private async Task OnFileChange(UploadFile file)
    {
        if (file.File != null)
        {
            //保存证书文件
            var filePath = Path.Combine(WebHostEnvironment.WebRootPath, $"upload/{file.File.Name}");
            await file.SaveToFile(filePath);
            //读取文件并且转换为BASE64字符串存储
            using var fs = new FileStream(filePath, FileMode.Open);
            fs.Seek(0, SeekOrigin.Begin);
            var buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            Model.CertificateFileName = file.File.Name;
            Model.CertificateFile = Convert.ToBase64String(buffer);
        }
        else
        {
            Model.CertificateFileName = "";
            Model.CertificateFile = "";
        }
    }
    private Task OnInvalidSubmitAsync(EditContext context)
    {
        return Task.CompletedTask;
    }
    private async Task OnSaveAsync(EditContext context)
    {
        var result= await AuthorizationAppService.SaveAuthenticationAsync(Model);
        if (result.Code != 0)
        {
            await ToastService.Warning(result.Message);
        }
        else
        {
            await OnClickCallback.InvokeAsync();
            await ToastService.Success("保存成功");
        }
    }
}
